# Maintainer: Jan Alexander Steffens (heftig) <heftig@archlinux.org>
# Contributor: Jan de Groot <jgc@archlinux.org>

pkgbase=gvfs
pkgname=(
  gvfs
  gvfs-{smb,afc,gphoto2,goa,mtp,nfs,google}
)
pkgver=1.50.4
pkgrel=3
pkgdesc="Virtual filesystem implementation for GIO"
url="https://wiki.gnome.org/Projects/gvfs"
arch=(x86_64)
license=(LGPL)
depends=(
  avahi
  dconf
  fuse3
  gcr
  gsettings-desktop-schemas
  libarchive
  libbluray
  libcdio-paranoia
  libgudev
  libsecret
  libsoup3
  psmisc
  udisks2
)
makedepends=(
  dbus
  docbook-xsl
  git
  gtk-doc
  gtk3
  libgdata
  libgoa
  libgphoto2
  libimobiledevice
  libmtp
  libnfs
  meson
  openssh
  python
  smbclient
)
groups=(gnome)
_commit=e1d4d719af002a603abb440c5ab3b0d281757d74  # tags/1.50.4^0
source=(
  "git+https://gitlab.gnome.org/GNOME/gvfs.git#commit=$_commit"
  gvfsd.hook
)
b2sums=('SKIP'
        '08a830600964d2c896d7f107d9558053a32eb6b9166433d6e29b58d4c38e318cbb7fdf7b8230cf72d433df65f4698b9ad86624bf7d3c30c883db325d51335189')

pkgver() {
  cd gvfs
  git describe --tags | sed 's/[^-]*-g/r&/;s/-/+/g'
}

prepare() {
  cd gvfs
}

build() {
  arch-meson gvfs build -D man=true
  meson compile -C build
}

check() {
  meson test -C build --print-errorlogs
}

_pick() {
  local p="$1" f d; shift
  for f; do
    d="$srcdir/$p/${f#$pkgdir/}"
    mkdir -p "$(dirname "$d")"
    mv "$f" "$d"
    rmdir -p --ignore-fail-on-non-empty "$(dirname "$f")"
  done
}

package_gvfs() {
  replaces=(gvfs-{obexftp,afp})
  optdepends=(
    'gtk3: Recent files support'
    'gvfs-afc: AFC (mobile devices) support'
    'gvfs-goa: gnome-online-accounts (e.g. OwnCloud) support'
    'gvfs-google: Google Drive support'
    'gvfs-gphoto2: gphoto2 (PTP camera/MTP media player) support'
    'gvfs-mtp: MTP device support'
    'gvfs-nfs: NFS support'
    'gvfs-smb: SMB/CIFS (Windows client) support'
  )

  meson install -C build --destdir "$pkgdir"

  ( cd "$pkgdir"

    _pick smb \
      usr/lib/gvfsd-smb{,-browse} \
      usr/share/GConf/gsettings/gvfs-smb.convert \
      usr/share/glib-2.0/schemas/org.gnome.system.smb.gschema.xml \
      usr/share/gvfs/mounts/smb{,-browse}.mount

    _pick afc \
      usr/lib/gvfs{-afc-volume-monitor,d-afc} \
      usr/lib/systemd/user/gvfs-afc-volume-monitor.service \
      usr/share/dbus-1/services/org.gtk.vfs.AfcVolumeMonitor.service \
      usr/share/gvfs/mounts/afc.mount \
      usr/share/gvfs/remote-volume-monitors/afc.monitor

    _pick gphoto2 \
      usr/lib/gvfs{-gphoto2-volume-monitor,d-gphoto2} \
      usr/lib/systemd/user/gvfs-gphoto2-volume-monitor.service \
      usr/share/dbus-1/services/org.gtk.vfs.GPhoto2VolumeMonitor.service \
      usr/share/gvfs/mounts/gphoto2.mount \
      usr/share/gvfs/remote-volume-monitors/gphoto2.monitor

    _pick mtp \
      usr/lib/gvfs{-mtp-volume-monitor,d-mtp} \
      usr/lib/systemd/user/gvfs-mtp-volume-monitor.service \
      usr/share/dbus-1/services/org.gtk.vfs.MTPVolumeMonitor.service \
      usr/share/gvfs/mounts/mtp.mount \
      usr/share/gvfs/remote-volume-monitors/mtp.monitor

    _pick goa \
      usr/lib/gvfs-goa-volume-monitor \
      usr/lib/systemd/user/gvfs-goa-volume-monitor.service \
      usr/share/dbus-1/services/org.gtk.vfs.GoaVolumeMonitor.service \
      usr/share/gvfs/remote-volume-monitors/goa.monitor

    _pick nfs \
      usr/lib/gvfsd-nfs \
      usr/share/gvfs/mounts/nfs.mount

    _pick google \
      usr/lib/gvfsd-google \
      usr/share/gvfs/mounts/google.mount

    install -d -o root -g 102 -m 750 usr/share/polkit-1/rules.d
  )

  install -Dt "$pkgdir/usr/share/libalpm/hooks" -m644 *.hook
}

package_gvfs-smb() {
  pkgdesc+=" (SMB/CIFS backend; Windows client)"
  depends=(
    "gvfs=$pkgver"
    'smbclient>=4.12.0'
  )

  mv smb/* "$pkgdir"
}

package_gvfs-afc() {
  pkgdesc+=" (AFC backend; Apple mobile devices)"
  depends=(
    "gvfs=$pkgver"
    libimobiledevice
    usbmuxd
  )

  mv afc/* "$pkgdir"
}

package_gvfs-gphoto2() {
  pkgdesc+=" (gphoto2 backend; PTP camera, MTP media player)"
  depends=(
    "gvfs=$pkgver"
    libgphoto2
  )

  mv gphoto2/* "$pkgdir"
}

package_gvfs-mtp() {
  pkgdesc+=" (MTP backend; Android, media player)"
  depends=(
    "gvfs=$pkgver"
    libmtp
  )

  mv mtp/* "$pkgdir"
}

package_gvfs-goa() {
  pkgdesc+=" (Gnome Online Accounts backend; cloud storage)"
  depends=(
    "gvfs=$pkgver"
    libgoa
  )

  mv goa/* "$pkgdir"
}

package_gvfs-nfs() {
  pkgdesc+=" (NFS backend)"
  depends=(
    "gvfs=$pkgver"
    libnfs
  )
  install=gvfs-nfs.install

  mv nfs/* "$pkgdir"
}

package_gvfs-google() {
  pkgdesc+=" (Google Drive backend)"
  depends=(
    "gvfs-goa=$pkgver"
    libgdata
  )

  mv google/* "$pkgdir"
}

# vim:set sw=2 sts=-1 et:
